﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 获取批次的详细信息
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("BATCH", "GetDetail", "1.0")]
    public class GetDetailAction : BaseAction<GetDetailParams, GetDetailResult>
    {
        protected override ActionResult<GetDetailResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetDetailParams opParams)
        {
            SER.BatchService service = new SER.BatchService();

            ServiceResult<VIW.VBatchDetail> serResult = service.GetDetail(opParams.BatchId);

            if(serResult.IsSuccess)
            {
                GetDetailResult result = new GetDetailResult();
                result.BatchId = serResult.Data.Batch.Id;
                for (int dspIndex = 0; dspIndex < serResult.Data.SourceParameters.Count; dspIndex++)
                {
                    GetDetailResult.Parameter parameter = new GetDetailResult.Parameter();
                    parameter.Name = serResult.Data.SourceParameters[dspIndex].Name;
                    parameter.Type = serResult.Data.SourceParameters[dspIndex].DataType;

                    for (int bpIndex = 0; bpIndex < serResult.Data.Parameters.Count; bpIndex++)
                    {
                        if(serResult.Data.SourceParameters[dspIndex].Id == serResult.Data.Parameters[bpIndex].Id)
                        {
                            parameter.Value = serResult.Data.Parameters[bpIndex].ValueString;
                            break;
                        }
                    }

                    result.Parameters.Add(parameter);
                }

                return new ActionResult<GetDetailResult>(result);
            }
            else
            {
                return new ActionResult<GetDetailResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetDetailParams
    {
        public Guid BatchId { get; set; }
    }

    public class GetDetailResult
    {
        public Guid BatchId { get; set; }

        public List<Parameter> Parameters = new List<Parameter>();

        public class Parameter
        {
            public string Name { get; set; }

            public string Type { get; set; }

            public string Value { get; set; }
        }
    }
}